<template>
  <div>
    <div v-if="stat===1">{{msg}}</div>
    <div v-if="stat===2">登录失败，点击<a href="/loginsso1">这里</a>重试</div>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
export default {
  name: 'LoginSSO2',
  data() {
    return {
      stat: 1,
      msg: '正在验证用户信息，请稍候...'
    }
  },
  mounted() {
    this.doLogin()
  },
  methods: {
    ...mapActions('account', ['Login']),
    getUrlKey(name) {
      // eslint-disable-next-line no-sparse-arrays
      return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
    },
    doLogin() {
      const username = this.getUrlKey('code')
      const password = 'shgdfhggdfadfdsgfdhkhgtetwrresfgfdgh'
      this.Login({
        username,
        password
      }).then(this.afterLogin).catch(() => {
        this.stat = 2
      })
    },
    afterLogin(res) {
      if (res.code === 200) {
        this.msg = '认证成功，正在为您跳转至首页'
        this.$router.push({ path: '/dashboard/analysis' })
      } else {
        this.stat = 2
      }
    }
  }
}
</script>
